<?php defined('SYSPATH') OR die('No direct access allowed.');

class Member_Controller extends Template_Controller {
	// Disable this controller when Kohana is set to production mode.
	// See http://docs.kohanaphp.com/installation/deployment for more details.
	const ALLOW_PRODUCTION = FALSE;

	// Set the name of the template to use
	public $template = 'car2club/template';
	
	public function index(){
		
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('member/member_content');

		//sakorn.s test get query
		$this->template->content->data_member_s=ORM::factory("member")->find_all();
		$this->template->content->data_contries=ORM::factory("contrie")->find_all();
		// You can assign anything variable to a view by using standard OOP
		// methods. In my welcome view, the $title variable will be assigned
		// the value I give it here.
		$this->template->title = 'สมัครสมาชิก / Register';

		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
		$this->template->content->links = array
		(
			'Home Page'     => 'http://kohanaphp.com/',
			'Documentation' => 'http://docs.kohanaphp.com/',
			'Forum'         => 'http://forum.kohanaphp.com/',
			'License'       => 'Kohana License.html',
			'Donate'        => 'http://kohanaphp.com/donate',
		);
	}
	
	
	public function getMemberAll(){
		// Look at the counters for valid and invalid
		// responses in the Session Profiler.
		new Profiler;
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('member/show_member_query');
		
		$this->template->content->data_member_s=ORM::factory("member")->find_all();
		

		// You can assign anything variable to a view by using standard OOP
		// methods. In my welcome view, the $title variable will be assigned
		// the value I give it here.
		$this->template->title = '';

		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
		$this->template->content->links = array
		(
			'Home Page'     => 'http://kohanaphp.com/',
			'Documentation' => 'http://docs.kohanaphp.com/',
			'Forum'         => 'http://forum.kohanaphp.com/',
			'License'       => 'Kohana License.html',
			'Donate'        => 'http://kohanaphp.com/donate',
		);
		//$this->template->content->render(true);
	}
	
	public function getKeySEQ(){
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('member/show_key_seq');
		
		//#######################################################
		//Begin Insert Member
		//#######################################################
		$sql="delete from seq_members";
		Database::instance()->query($sql);
				
		$seq_members = new Seq_Member_Model();
		$seq_members->ip_address="0.0.0.0";
		$seq_members->save();
		
		$this->template->content->seq_member=ORM::factory("seq_member")->find_all();
		
		//#######################################################
		//End Insert Member
		//#######################################################
		
		
		// You can assign anything variable to a view by using standard OOP
		// methods. In my welcome view, the $title variable will be assigned
		// the value I give it here.
		$this->template->title = 'get key seq!';

		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
		$this->template->content->links = array
		(
			'Home Page'     => 'http://kohanaphp.com/',
			'Documentation' => 'http://docs.kohanaphp.com/',
			'Forum'         => 'http://forum.kohanaphp.com/',
			'License'       => 'Kohana License.html',
			'Donate'        => 'http://kohanaphp.com/donate',
		);
	}
	public function registerMember(){
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('member/show_member');
		
		//request param from memberForm
		//1.members table
		$this->template->content->prefix=$this->input->post("prefix");
		$this->template->content->first_name=$this->input->post("first_name");
		$this->template->content->last_name=$this->input->post("last_name");
		$this->template->content->id_card=$this->input->post("id_card");
		$this->template->content->sex=$this->input->post("sex");
		$this->template->content->telephone=$this->input->post("telephone");
		$this->template->content->email=$this->input->post("email");
		//2.address_members table
		//$this->template->content->contie_id=$this->input->post("contie_id");
		$this->template->content->address=$this->input->post("address");
		$this->template->content->province=$this->input->post("province");
		$this->template->content->post=$this->input->post("post");
		
		//#######################################################
		//Begin Insert Member
		//#######################################################
		$sql="delete from seq_members";
		Database::instance()->query($sql);
				
		$seq_members = new Seq_Member_Model();
		$seq_members->ip_address="0.0.0.0";
		$seq_members->save();
		
		$seq_member=ORM::factory("seq_member")
					->find_all();
		$seqid="";
		foreach ( $seq_member as $value ) {
       		$seqid=$value->id;
       		break;
		}		
		$member = new Member_Model();
		$member->id=$seqid;
		$member->address_member_id=$seqid;
		$member->prefix=$this->input->post("prefix");
		$member->first_name=$this->input->post("first_name");
		$member->last_name=$this->input->post("last_name");
		$member->id_card=$this->input->post("id_card");
		$member->sex=$this->input->post("sex");
		$member->telephone=$this->input->post("telephone");
		$member->email=$this->input->post("email");
		$member->status="Y";
		$member->deleted="N";
		$member->save();
		
		$address_member = new Address_Member_Model();
		$address_member->id=$seqid;
		$address_member->contrie_id=$this->input->post("contrie_id");
		$address_member->address=$this->input->post("address");
		$address_member->province=$this->input->post("province");
		$address_member->post=$this->input->post("post");
		$address_member->save();
		//#######################################################
		//End Insert Member
		//#######################################################
		
		
		// You can assign anything variable to a view by using standard OOP
		// methods. In my welcome view, the $title variable will be assigned
		// the value I give it here.
		$this->template->title = 'แสดงข้อมูลสมาชิก!';

		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
		$this->template->content->links = array
		(
			'Home Page'     => 'http://kohanaphp.com/',
			'Documentation' => 'http://docs.kohanaphp.com/',
			'Forum'         => 'http://forum.kohanaphp.com/',
			'License'       => 'Kohana License.html',
			'Donate'        => 'http://kohanaphp.com/donate',
		);
	}
	
	public function insertMember(){
		getPKMember();
		$seq_member=ORM::factory("seq_member")
					->limit(1)
					->find_all();
		$member = new Member_Model();
		$member->id=$seq_member->id;
		$member->prefix=$this->input->post("prefix");
		$member->first_name=$this->input->post("first_name");
		$member->last_name=$this->input->post("last_name");
		$member->id_card=$this->input->post("id_card");
		$member->sex=$this->input->post("sex");
		$member->telephone=$this->input->post("telephone");
		$member->email=$this->input->post("email");
		$member->save();
	}
	
	public function getPKMember(){
		$seq_member = new Seq_Member_Model();
		$seq_member->ip_address="0.0.0.0";
		$seq_member->save();
	}
	
	public function memberForm(){
		// In Kohana, all views are loaded and treated as objects.
		$this->template->content = new View('member/member_form');

		//sakorn.s test get query
		//$this->template->content->data_member_s=ORM::factory("member")->find_all();
		//$this->template->content->data_contries=ORM::factory("contrie")->find_all();
		// You can assign anything variable to a view by using standard OOP
		// methods. In my welcome view, the $title variable will be assigned
		// the value I give it here.
		$this->template->title = 'ทดสอบ ฟอร์ม JQuery!';

		// An array of links to display. Assiging variables to views is completely
		// asyncronous. Variables can be set in any order, and can be any type
		// of data, including objects.
		$this->template->content->links = array
		(
			'Home Page'     => 'http://kohanaphp.com/',
			'Documentation' => 'http://docs.kohanaphp.com/',
			'Forum'         => 'http://forum.kohanaphp.com/',
			'License'       => 'Kohana License.html',
			'Donate'        => 'http://kohanaphp.com/donate',
		);
	}
}
?>
